--[[
--
-- BigWigs Strategy Module 
--
-- Warns the raid when someone is Dazed.
--
--]]

------------------------------
--      Are you local?      --
------------------------------

local name = "Zombie Food"
local L = AceLibrary("AceLocale-2.2"):new("BigWigs"..name)

local times = nil

L:RegisterTranslations("enUS", function() return {
	["Zombie Food"] = true,
	["zombiefood"] = true,
	["Optionally broadcast the raid when someone gets Dazed."] = true,
	["broadcast"] = true,
	["Broadcast"] = true,
	["Toggle broadcasting."] = true,
	["On"] = true,
	["on"] = true,
	["Toggles the plugin on and off."] = true,

	["%s has been Dazed!"] = true,
	["Dazed"] = true,
} end )

L:RegisterTranslations("zhCN", function() return {
	["Zombie Food"] = "眩晕警告器",
	["zombiefood"] = "眩晕警告器",
	["Optionally broadcast the raid when someone gets Dazed."] = "当某玩家眩晕时将会向团队广播。",
	["broadcast"] = "广播",
	["Broadcast"] = "广播",
	["Toggle broadcasting."] = "切换广播。",
	["On"] = "打开",
	["on"] = "打开",
	["Toggles the plugin on and off."] = "是否使用此插件。",

	["%s has been Dazed!"] = "%s受到了眩晕效果的影响",
	["Dazed"] = "眩晕",
} end )

BigWigsZombieFood = BigWigs:NewModule(name)
BigWigsZombieFood.defaultDB = {
	broadcast = false,
	on = true,
}

BigWigsZombieFood.consoleCmd = L["zombiefood"]
BigWigsZombieFood.consoleOptions = {
	type = "group",
	name = L["Zombie Food"],
	desc = L["Optionally broadcast the raid when someone gets Dazed."],
	args   = {
		[L["on"]] = {
			type = "toggle",
			name = L["On"],
			desc = L["Toggles the plugin on and off."],
			get = function() return BigWigsZombieFood:IsEventRegistered("UNIT_AURA") end,
			set = function(v)
				BigWigsZombieFood.db.profile.on = v
				if v then
					BigWigsZombieFood:RegisterEvent("UNIT_AURA")
				else
					BigWigsZombieFood:UnregisterEvent("UNIT_AURA")
				end
			end,
		},
		[L["broadcast"]] = {
			type = "toggle",
			name = L["Broadcast"],
			desc = L["Toggle broadcasting."],
			get = function() return BigWigsZombieFood.db.profile.broadcast end,
			set = function(v) BigWigsZombieFood.db.profile.broadcast = v end,
		},
	}
}
BigWigsZombieFood.revision = tonumber(string.sub("$Revision: 46 $", 12, -3))
BigWigsZombieFood.external = true

function BigWigsZombieFood:OnEnable()
	if self.db.profile.on then
		self:RegisterEvent("UNIT_AURA")
	end
	times = {}
end

function BigWigsZombieFood:UNIT_AURA(unit)
	if not unit or not UnitInRaid(unit) then return end
	local i = 1
	local name = UnitDebuff(unit, i)
	while name do
		if name == L["Dazed"] then
			local n = UnitName(unit)
			if not times[n] or (times[n] + 5) < GetTime() then
				self:TriggerEvent("BigWigs_Message", string.format(L["%s has been Dazed!"], n), "Orange", not self.db.profile.broadcast)
				times[n] = GetTime()
			end
			break
		end
		i = i + 1
		name = UnitDebuff(unit, i)
	end
end

